package com.meixiang.adapter.myFundAdapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.fund.FundProfitRecord;
import com.meixiang.recyclerview.RecyclerAdapter;
import com.meixiang.util.AbStrUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 收益记录适配器
 * author:xyj
 */
public class EarningsRecordAdapter extends RecyclerAdapter<EarningsRecordAdapter.ViewHolder> {
    private Context mContext;
    private static final int TYPE_HEADER = 0; // 头部视图标志
    private static final int TYPE_NORMAL = 1; // 内容视图标志
    private View mHeaderView; //頭部
    private int mHeadCount = 1; //头部视图的个数
    private List<FundProfitRecord> mProfitRecord;

    public EarningsRecordAdapter(Context context) {
        this.mContext = context;
    }

    public void refreshData(List<FundProfitRecord> mProfitRecord) {
        this.mProfitRecord = mProfitRecord;
        notifyDataSetChanged();
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    /**
     * 判断当前Item的类型
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) {
            return new ViewHolder(mHeaderView);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_fund_detial_earnings_record_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(holder);
        if ("0".equals(mProfitRecord.get(pos).getDay_profit())) {
            holder.tv_day_rate.setTextColor(ContextCompat.getColor(mContext,R.color.black_9));
        }
        if (!"0.00".equals(mProfitRecord.get(pos).getDay_rate())) {
            if (mProfitRecord.get(pos).getDay_rate().contains("-")) {
                if (mProfitRecord.get(pos).getDay_profit().contains("-")) {
                    holder.tv_day_rate.setText(AbStrUtil.ChangeF3D(mProfitRecord.get(pos).getDay_profit()));
                    holder.tv_day_profit.setText("(" + mProfitRecord.get(pos).getDay_rate() + "% )");

                } else {
                    holder.tv_day_rate.setText("+" + AbStrUtil.ChangeF3D(mProfitRecord.get(pos).getDay_profit()));
                    holder.tv_day_profit.setText("(" + mProfitRecord.get(pos).getDay_rate() + "% )");
                }
                holder.tv_day_profit.setTextColor(ContextCompat.getColor(mContext,R.color.fund_confirm_color));
            } else {
                if (mProfitRecord.get(pos).getDay_profit().contains("-")) {
                    holder.tv_day_rate.setText(AbStrUtil.ChangeF3D(mProfitRecord.get(pos).getDay_profit()));
                    holder.tv_day_profit.setText("(+" + mProfitRecord.get(pos).getDay_rate() + "%)");
                } else {
                    holder.tv_day_rate.setText("+" + AbStrUtil.ChangeF3D(mProfitRecord.get(pos).getDay_profit()));
                    holder.tv_day_profit.setText("(+" + mProfitRecord.get(pos).getDay_rate() + "%)");
                }
            }
        } else {
            if (mProfitRecord.get(pos).getDay_rate().contains("-")) {
                if (mProfitRecord.get(pos).getDay_profit().contains("-")) {
                    holder.tv_day_rate.setText(AbStrUtil.ChangeF3D(mProfitRecord.get(pos).getDay_profit()));
                } else {
                    holder.tv_day_rate.setText("+" + AbStrUtil.ChangeF3D(mProfitRecord.get(pos).getDay_profit()));
                }
            } else {
                if (mProfitRecord.get(pos).getDay_profit().contains("-")) {
                    holder.tv_day_rate.setText(AbStrUtil.ChangeF3D(mProfitRecord.get(pos).getDay_profit()));
                } else {
                    holder.tv_day_rate.setText("+" + AbStrUtil.ChangeF3D(mProfitRecord.get(pos).getDay_profit()));
                }
            }
            holder.tv_day_profit.setText("(+" + mProfitRecord.get(pos).getDay_rate() + "%)");
        }
        holder.tv_nav_date.setText(mProfitRecord.get(pos).getNav_date());
        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(holder.itemView, pos);

                }
            });
        }
    }

    /**
     * 得到真實的position
     *
     * @param holder
     * @return
     */
    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public int getItemCount() {
        return mProfitRecord == null ? mHeadCount : mProfitRecord.size() + mHeadCount;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_day_rate)
        TextView tv_day_rate;
        @Bind(R.id.tv_nav_date)
        TextView tv_nav_date;
        @Bind(R.id.day_profit)
        TextView tv_day_profit;

        ViewHolder(View view) {
            super(view);
            if (view == mHeaderView) return;
            ButterKnife.bind(this, view);
        }
    }

}
